package com.cloud.school.bus.teacherhelper.modules.stuclass.adapter;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.android.support.jhf.utils.DateUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.entitys.Student;

import android.R.integer;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AddStudentExistAdapter extends BaseAdapter {

	private Context mContext;
	private List<Student> mStudents;
	private Map<Integer, Boolean> mMap = new HashMap<Integer, Boolean>();
	private int mLastIndex = -1;
	private int mCurYear = 0;
	private Calendar mBirthdayCalendar = Calendar.getInstance();

	public AddStudentExistAdapter(Context context, List<Student> students) {
		mContext = context;
		mStudents = students;
		Calendar curCalendar = Calendar.getInstance();
		curCalendar.setTimeInMillis(System.currentTimeMillis());
		mCurYear = curCalendar.get(Calendar.YEAR);
		
		clearSelected();
	}

	@Override
	public int getCount() {
		return mStudents.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mStudents.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			arg1 = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_add_student_exist, null);
		}

		Student student = mStudents.get(arg0);

		TextView nameTextView = (TextView) arg1.findViewById(R.id.nameTextView);
		nameTextView.setText(student.cnname);
		TextView parentPhoneTextView = (TextView) arg1
				.findViewById(R.id.parentPhoneTextView);
		parentPhoneTextView.setText(student.mobile);
		TextView ageTextView = (TextView) arg1.findViewById(R.id.ageTextView);
		if (!TextUtils.isEmpty(student.age)) {
			ageTextView.setText(student.age + mContext.getString(R.string.age));
		} else {
			mBirthdayCalendar.setTimeInMillis(DateUtils.getDateMillis(student.birthday, "yyyy-MM-dd"));
			int age = mCurYear - mBirthdayCalendar.get(Calendar.YEAR);
			ageTextView.setText(age + mContext.getString(R.string.age));
		}
		TextView sexTextView = (TextView) arg1.findViewById(R.id.sexTextView);
		if ("1".equals(student.sex)) {
			sexTextView.setText(mContext.getString(R.string.prince));
		} else if ("2".equals(student.sex)) {
			sexTextView.setText(mContext.getString(R.string.princess));
		}
		TextView classNameTextView = (TextView) arg1
				.findViewById(R.id.classNameTextView);
		classNameTextView.setText(student.classname);
		ImageView rightImageView = (ImageView) arg1
				.findViewById(R.id.rightImageView);
		rightImageView.setSelected(mMap.get(arg0));

		return arg1;
	}

	private void clearSelected() {
		for (int i = 0; i < mStudents.size(); i++) {
			mMap.put(i, false);
		}
	}
	
	/**
	 * 返回当前选中的index
	 * @return
	 */
	public int getCurrentItem(){
		int index = -1;
		for (int i = 0; i < mMap.size(); i++) {
			if(mMap.get(i)){
				index = i;
			}
		}
		return index;
	}

	/**
	 * 
	 * @param index
	 * @return 返回表示当前item是否选中
	 */
	public boolean setSelectedIndex(int index) {
		boolean result = !mMap.get(index);
		mMap.put(index, result);
		if (-1 != mLastIndex && index != mLastIndex)
			mMap.put(mLastIndex, false);
		mLastIndex = index;
		notifyDataSetChanged();
		return result;
	}

}
